//
//  Tools.m
//  find
//
//  Created by 吕超 on 16/12/27.
//  Copyright © 2016年 ywydz. All rights reserved.
//

#import "Tools.h"

#import "AFNetworking.h"

//正式 网址
#define ZSWaiWangUrl @"http://app.51zzj.com.cn/api/"


@implementation Tools

// 当回到原来的视图时, 如果希望这个视图还是竖屏, 可强制旋转一下
//强制旋转屏幕
+ (void)orientationToPortrait:(UIInterfaceOrientation)orientation{
    
    SEL selector = NSSelectorFromString(@"setOrientation:");
    
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
    
    [invocation setSelector:selector];
    
    [invocation setTarget:[UIDevice currentDevice]];
    
    int val = orientation;
    
    [invocation setArgument:&val atIndex:2];//前两个参数已被target和selector占用
    
    [invocation invoke];
}


#pragma mark ====  时间戳转换
+ (NSString *)timeStampConversionTime:(NSString *)str{
    
    NSString *time = [NSString stringWithFormat:@"%@", str];
    
    NSInteger num = [time doubleValue]/1000;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"YYYY/MM/dd"];
    
    NSDate * confromTimesp = [NSDate dateWithTimeIntervalSince1970:num];
    
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    
    return confromTimespStr;
}


#pragma mark  ======  修改图片大小
+ (UIImage*)OriginImage:(UIImage *)image scaleToSize:(CGSize)size{
    UIGraphicsBeginImageContext(size);  //size 为CGSize类型，即你所需要的图片尺寸
    
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return scaledImage;   //返回的就是已经改变的图片
}



#pragma mark ====  补全网络请求字符串  (内网)
+ (NSString *)RequestnetworkString:(NSString *)str{
    NSString *urlStr;
    urlStr = [NSString stringWithFormat:@"%@%@",ZSWaiWangUrl,str];
    return urlStr;
}


+ (UIAlertController *)showAlertController:(UIViewController *)vc{
    
    UIAlertController *xwAlertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请先登录!" preferredStyle:UIAlertControllerStyleAlert];
    
    //默认UIAlertAction  取消按钮
    UIAlertAction *defaultAlertAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:nil];
    
    [xwAlertController addAction:defaultAlertAction];
    
    [vc presentViewController:xwAlertController animated:YES completion:nil];
    
    return xwAlertController;
}




// GET
+ (void)GET:(NSString *)urlString parameters:(id)parameters success:(void (^) (id responseObject))success failure:(void (^) (NSError *error))failure{
    
    
    // 初始化管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
      manager.responseSerializer = [AFJSONResponseSerializer serializer];//申明返回的结果是json类型
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/plain",@"text/json",@"text/javascript",@"xml/json", nil];
    manager.requestSerializer.timeoutInterval = 60;
    
    [manager GET:urlString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}




// POST
+ (void)POST:(NSString *)urlString parameters:(id)parameters success:(void (^)(id))success failure:(void (^) (NSError *))failure{
  

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"text/html", @"application/json",@"text/json",@"text/javascript",@"json/xml", nil];
    
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    manager.responseSerializer = [AFJSONResponseSerializer serializer];//申明返回的结果是json类型

    [manager.requestSerializer  setValue:@"application/json"  forHTTPHeaderField:@"Content－Type"];
    
    manager.requestSerializer.timeoutInterval = 60.f;
    
    [manager POST:urlString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success ) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}







@end
